import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;
import javax.swing.JOptionPane;

/**
 *
 * @author jolynn
 */
public class Edit extends javax.swing.JPanel {
    JPanel mainPanel;
    Connection con;
    
    private String timeType = null, reminder = null, rHour = null, rMin = null, rTimeType = null;
    private String rDate = null, rMonth = null, rYear = null, title = null, hour = null;
    private String min = null, getCode = null;
    
    private String hourBox [] = { "HOUR", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
    private String minBox [] = { "MIN", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
    private String timeTypeBox [] = { "AM", "PM" };
    private String reminderBox [] = { " ", "YES", "NO" };
    private String rHourBox [] = { "HOUR", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
    private String rMinBox [] = { "MIN", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
    private String rTimeTypeBox [] = { "AM", "PM" };
    private String rDateBox [] = { "DATE", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };
    private String rMonthBox [] = { "MONTH", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
    private String rYearBox [] = { "YEAR", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035", "2036", "2037", "2038", "2039", "2040", "2041", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050", "2051", "2052", "2053", "2054", "2055", "2056", "2057", "2058", "2059", "2060", "2061", "2062", "2063", "2064", "2065", "2066", "2067", "2068", "2069", "2070", "2071", "2072", "2073", "2074", "2075", "2076", "2077", "2078", "2079", "2080", "2081", "2082", "2083", "2084", "2085", "2086", "2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094", "2095", "2096", "2097", "2098", "2099", "2100", "2101", "2102", "2103", "2104", "2105", "2106", "2107", "2108", "2109", "2110" };
    
    
    public Edit(JPanel panel) {
    	mainPanel = panel;
    	
    	try {
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/user", "user", "user");
            System.out.println("DB connected from EditEntry");
            
        } catch (SQLException ex) {
        	//Logger.getLogger(ConnectingDatabase.class.getName()).log(Level.SEVERE, null, ex);
        	System.out.println(ex.getMessage());
        }
        
        initComponents();
        
        backButton.addActionListener(new PanelSwitchToEntryMenu());
    }
    
    @SuppressWarnings("unchecked")
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        EditTitle = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        hourComboBox = new javax.swing.JComboBox();
        minComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        venueTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        timeTypeComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        reminderComboBox = new javax.swing.JComboBox();
        rHourComboBox = new javax.swing.JComboBox();
        rMinComboBox = new javax.swing.JComboBox();
        rTimeTypeComboBox = new javax.swing.JComboBox();
        backButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        rDateComboBox = new javax.swing.JComboBox();
        rMonthComboBox = new javax.swing.JComboBox();
        rYearComboBox = new javax.swing.JComboBox();
        editTitleComboBox = new javax.swing.JComboBox();
        

        jPanel1.setFont(new java.awt.Font("DejaVu Sans", 0, 15));
        
        EditTitle.setFont(new java.awt.Font("DejaVu Sans", 1, 36)); // NOI18N
        EditTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        EditTitle.setText("EDIT ENTRY");
        
        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("TITLE :");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("TIME :");
        
        hourComboBox.setModel(new javax.swing.DefaultComboBoxModel(hourBox));
        hourComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hourComboBoxActionPerformed(evt);
            }
        });
        
        minComboBox.setModel(new javax.swing.DefaultComboBoxModel(minBox ));
        minComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minComboBoxActionPerformed(evt);
            }
        });
        
        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("VENUE :");

        venueTextField.setFont(new java.awt.Font("DejaVu Sans", 0, 16)); // NOI18N

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("DESCRIPTION :");

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setFont(new java.awt.Font("DejaVu Sans", 0, 16)); // NOI18N
        descriptionTextArea.setRows(5);
        jScrollPane1.setViewportView(descriptionTextArea);

        timeTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(timeTypeBox));
        timeTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timeTypeComboBoxActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("REMINDER :");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("REMINDER TIME :");

        reminderComboBox.setModel(new javax.swing.DefaultComboBoxModel(reminderBox));
        reminderComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	reminderComboBoxActionPerformed(evt);
            }
        });
        
        rHourComboBox.setModel(new javax.swing.DefaultComboBoxModel(rHourBox));
        rHourComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rHourComboBoxActionPerformed(evt);
            }
        });
        
        rMinComboBox.setModel(new javax.swing.DefaultComboBoxModel(rMinBox));
        rMinComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rMinComboBoxActionPerformed(evt);
            }
        });
        
        rTimeTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(rTimeTypeBox));
        rTimeTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rTimeTypeComboBoxActionPerformed(evt);
            }
        });
        
        backButton.setText("BACK");

        saveButton.setText("SAVE");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	saveButtonActionPerformed(evt);
            }
        });
        
        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 16)); // NOI18N
        jLabel1.setText("REMINDER DATE :");

        rDateComboBox.setModel(new javax.swing.DefaultComboBoxModel(rDateBox ));
        rDateComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rDateComboBoxActionPerformed(evt);
            }
        });
        
        rMonthComboBox.setModel(new javax.swing.DefaultComboBoxModel(rMonthBox));
        rMonthComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rMonthComboBoxActionPerformed(evt);
            }
        });
        
        rYearComboBox.setModel(new javax.swing.DefaultComboBoxModel(rYearBox));
        rYearComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rYearComboBoxActionPerformed(evt);
            }
        });
        
        editTitleComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose Entry Title" }));
        editTitleComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	titleComboBoxActionPerformed(evt);
            }
        });
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(EditTitle, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 383, Short.MAX_VALUE)
                                .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(70, 70, 70)))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(50, 50, 50)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(venueTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(hourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(minComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(timeTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(editTitleComboBox, 0, 320, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))
                                .addGap(50, 50, 50)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(rDateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(rMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(rYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(rHourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(rMinComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(rTimeTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(reminderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(70, 70, 70))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(EditTitle)
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(editTitleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(hourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timeTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(venueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reminderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rHourComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(rMinComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rTimeTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(rDateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(backButton))
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }
    
    private void hourComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	hour = null;
    	hour = (String)hourComboBox.getSelectedItem();
    }
    
    private void minComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	min = null;
    	min = (String)minComboBox.getSelectedItem();
    }
    
    private void timeTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	timeType = null;
    	timeType = (String)timeTypeComboBox.getSelectedItem();
    }
    
    private void reminderComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	reminder = null;
    	reminder = (String)reminderComboBox.getSelectedItem();
    	
    	//If the user chooses NO, the combo box will not be enabled
    	if(reminder.equals("YES")) {
    		rHourComboBox.setEnabled(true);
    		rMinComboBox.setEnabled(true);
    		rTimeTypeComboBox.setEnabled(true);
    		rDateComboBox.setEnabled(true);
    		rMonthComboBox.setEnabled(true);
    		rYearComboBox.setEnabled(true);
    	}
    	else if(reminder.equals("NO")) {
    		rHourComboBox.setEnabled(false);
    		rMinComboBox.setEnabled(false);
    		rTimeTypeComboBox.setEnabled(false);
    		rDateComboBox.setEnabled(false);
    		rMonthComboBox.setEnabled(false);
    		rYearComboBox.setEnabled(false);
    	}
    }
    
    private void rHourComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rHour = null;
    	rHour = (String)rHourComboBox.getSelectedItem();
    }
    
    private void rMinComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rMin = null;
    	rMin = (String)rMinComboBox.getSelectedItem();
    }
    
    private void rTimeTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rTimeType = null;
    	rTimeType = (String)rTimeTypeComboBox.getSelectedItem();
    }
    
    private void rDateComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rDate = null;
    	rDate = (String)rDateComboBox.getSelectedItem();
    }
    
    private void rMonthComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rMonth = null;
    	rMonth = (String)rMonthComboBox.getSelectedItem();
    }

    private void rYearComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	rYear = null;
    	rYear = (String)rYearComboBox.getSelectedItem();
    }
    
    private void titleComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    	title = (String)editTitleComboBox.getSelectedItem();
    	String rTime = null, venue = null, descrip = null;
    	String d = null, m = null, y = null;
    	String hour = null, minute = null, amPM = null;
    	String rMinute = null, ramPM = null;
    	String rYesNo = null;
    	int checkHour = 0, rCheckHour = 0;
    	
    	String getEntryTime = null;
    	
    	try{
    		Statement stmt = con.createStatement();
    		ResultSet rset = stmt.executeQuery("SELECT * FROM ENTRY WHERE E_TITLE LIKE '" + title + "'");
    		
    		java.util.Date getReminderDate = null;
    		
    		while(rset.next()) {
    			getCode = rset.getString(1);
    			getEntryTime = rset.getString(4);
    			venue = rset.getString(5);
    			descrip = rset.getString(6);
    			getReminderDate = rset.getDate(7);
    			rTime = rset.getString(8);
    		}
    		
    		venueTextField.setText(venue);
			descriptionTextArea.setText(descrip);
			
			stmt.close();
    		
			//Entry Time
			//Change the entry hour from 24 hour system back to 12 hour system
			//  and set the time either AM or PM
			checkHour = Integer.parseInt(getEntryTime) / 100;
			if(checkHour >= 12 && checkHour < 24) {
				if(checkHour > 12) {
					checkHour -= 12;
					if(checkHour < 10)
						hour = ("0" + Integer.toString(checkHour));
					else
						hour = Integer.toString(checkHour);
				}
				else
					hour = "12";
				amPM = "PM";
			}
			else {
				if(checkHour < 10)
					hour = ("0" + Integer.toString(checkHour));
				else if(checkHour == 24)
					hour = "00";
				else
					hour = Integer.toString(checkHour);
				amPM = "AM";
			}
			
			//Set the entry hour in hourComboBox
    		for(int i = 0;i < hourBox.length;i++) {
    			if(hourBox[i].equals(hour)) 
    				hourComboBox.setSelectedIndex(i);
    		}
    		
    		//Change the entry minute from 24 hour system back to 12 hour system
    		minute = Integer.toString(Integer.parseInt(getEntryTime) % 100);
    		if(Integer.parseInt(minute) < 10)
    			minute = "0" + minute;
    		else if(Integer.parseInt(minute) == 60)
    			minute = "00";
    		
    		//Set the entry minute in minComboBox
    		for(int i = 0;i < minBox.length;i++) {
    			if(minBox[i].equals(minute)) 
    				minComboBox.setSelectedIndex(i);
    		}
    		
    		//Set the entry time according to whether it is AM or PM
    		for(int i = 0;i < timeTypeBox.length;i++) {
    			if(timeTypeBox[i].equals(amPM)) 
    				timeTypeComboBox.setSelectedIndex(i);
    		}
    		
    		
    		//Reminder Yes or No
    		String rDateCompare = new SimpleDateFormat("yyyy-MM-dd").format(getReminderDate);
    		if(rTime.equals("00:00:00") && rDateCompare.equals("2000-01-01"))
    			rYesNo = "NO"; //Reminder is NO if the rTime and rDate is the default reminder
    						   //  time and reminder date
    		else
    			rYesNo = "YES";
    		
    		//Set the reminder whether it is YES or NO
    		for(int i = 0;i < reminderBox.length;i++) {
    			if(reminderBox[i].equals(rYesNo)) 
    				reminderComboBox.setSelectedIndex(i);
    		}
    		
    		
    		//If the reminder is YES then set the reminder date and reminder time combo boxes
    		//  to the appropriate data read from the database
    		if(rYesNo.equals("YES")) {
    			//Reminder Date
    			d = new SimpleDateFormat("dd").format(getReminderDate);
    			for(int i = 0;i < rDateBox.length;i++) {
    				if(rDateBox[i].equals(d)) 
    					rDateComboBox.setSelectedIndex(i);
    			}
    		
    			m = new SimpleDateFormat("MM").format(getReminderDate);
    			for(int i = 0;i < rMonthBox.length;i++) {
    				if(rMonthBox[i].equals(m)) 
    					rMonthComboBox.setSelectedIndex(i);
    			}
    		
    			y = new SimpleDateFormat("yyyy").format(getReminderDate);
    			for(int i = 0;i < rYearBox.length;i++) {
    				if(rYearBox[i].equals(y)) 
    					rYearComboBox.setSelectedIndex(i);
    			}
    		
    		
    			//Reminder Time
    			//Get the hour from the time read by taking the first 2 char
    			rCheckHour = Integer.parseInt(rTime.substring(0,2));
    			if(rCheckHour > 12) {
    				rHour = Integer.toString(rCheckHour - 12);
    				if(Integer.parseInt(rHour) < 10)
    					rHour = "0" + rHour;
    				ramPM = "PM";
    			}
    			else {
    				rHour = Integer.toString(rCheckHour);
    				if(rHour.equals("0"))
    					rHour = "12";
    				else if(Integer.parseInt(rHour) < 10)
    					rHour = "0" + rHour;
    				else
    					rHour = Integer.toString(rCheckHour);
    				ramPM = "AM";
    			}
			
    			for(int i = 0;i < rHourBox.length;i++) {
    				if(rHourBox[i].equals(rHour)) 
    					rHourComboBox.setSelectedIndex(i);
    			}
    			
    			//Get the reminder minute from the time read by taking the
    			//  4th and 5th char
    			rMinute = rTime.substring(3,5);
    			if(rMinute.equals("0"))
    				rMinute = "00";
    		
    			for(int i = 0;i < rMinBox.length;i++) {
    				if(rMinBox[i].equals(rMinute)) 
    					rMinComboBox.setSelectedIndex(i);
    			}
    			
    			for(int i = 0;i < rTimeTypeBox.length;i++) {
    				if(rTimeTypeBox[i].equals(ramPM)) 
    					rTimeTypeComboBox.setSelectedIndex(i);
    			}
    		}
        }
        catch(Exception e){}
    }
    
    
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
        if(timeType == null || timeType.equals("AM")) {
        	timeType = "AM";
        	if(hour == "12")
        		hour = "00";
        }
        else if(timeType.equals("PM"))
        	hour = Integer.toString(Integer.parseInt(hour) + 12);
        
        
        if(reminder == null)
        	reminder = "NO";
        
        String entryTime = hour + min;
        
        
    	int save = JOptionPane.showConfirmDialog(this,"Save the changes made? ","Edit Entry",JOptionPane.YES_NO_OPTION);
        if(save == JOptionPane.YES_NO_OPTION){
            try{
            	Statement stmt = con.createStatement();
            	
            	//If reminder is YES, input the details as chosen by the user.
            	//If reminder is NO, the default reminder date and reminder time is executed
            	if(reminder.equals("YES")) {
            		if(rTimeType == null || rTimeType.equals("AM")) {
                    	rTimeType = "AM";
                    	if(rHour.equals("12"))
                    		rHour = "00";
                    }
                    else if(rTimeType.equals("PM")) {
                    	if(Integer.parseInt(rHour) < 12)
                    		rHour = Integer.toString(Integer.parseInt(rHour) + 12);
                    }
            		
            		String reminderDate =  rYear + "-" +  rMonth+ "-" +rDate  ;
            		String reminderTime = rHour + ":" + rMin + ":00";
            		
            		stmt.executeUpdate("UPDATE ENTRY SET" +
            			" E_TIME = " + entryTime + "," +
            			" E_VENUE = '" + venueTextField.getText() + "'," +
            			" E_DESCRIPTION = '" + descriptionTextArea.getText() + "'," +
            			" E_REMINDERDATE = '" + reminderDate + "'," +
            			" E_REMINDERTIME = '" + reminderTime + "'" +
            			" WHERE E_NUM = " + Integer.parseInt(getCode));
            	stmt.close();
            	}
            	else if (reminder.equals("NO")) {
            		String defaultDate = "2000-01-01";
                    
            		stmt.executeUpdate("UPDATE ENTRY SET" +
            			" E_TIME = " + entryTime + "," +
            			" E_VENUE = '" + venueTextField.getText() + "'," +
            			" E_DESCRIPTION = '" + descriptionTextArea.getText() + "'," +
            			" E_REMINDERDATE = '" + defaultDate + "'," +
            			" E_REMINDERTIME = '0000'" +
            			" WHERE E_NUM = " + Integer.parseInt(getCode));
            	stmt.close();
                	stmt.close();
            	}
            	
            	JOptionPane.showMessageDialog(this,"Your information have been saved","Edit Entry",JOptionPane.INFORMATION_MESSAGE);
            	
            	//Remove any input by the user
        		venueTextField.setText(null);
        		descriptionTextArea.setText(null);
        		
            	clearAllDetails();
            }
            catch(Exception e){
            	Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, e);
                JOptionPane.showMessageDialog(this,"WARNING : "+ e.getMessage(),"Edit Entry",JOptionPane.WARNING_MESSAGE);
            }
        }
    } 
    
    //Remove all inputs and details selected by the user in the GUI
    private void clearAllDetails() {
		//Put all combo box selection back to original
		editTitleComboBox.setSelectedIndex(0);
		hourComboBox.setSelectedIndex(0);
		minComboBox.setSelectedIndex(0);
		timeTypeComboBox.setSelectedIndex(0);
		reminderComboBox.setSelectedIndex(0);
    	rHourComboBox.setSelectedIndex(0);
		rMinComboBox.setSelectedIndex(0);
		rTimeTypeComboBox.setSelectedIndex(0);
		rDateComboBox.setSelectedIndex(0);
		rMonthComboBox.setSelectedIndex(0);
		rYearComboBox.setSelectedIndex(0);
    }
    
    class PanelSwitchToEntryMenu implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			//Remove any input by the user
			venueTextField.setText(null);
			descriptionTextArea.setText(null);
			
			clearAllDetails();
			
			CardLayout c = (CardLayout)(mainPanel.getLayout());
			c.show(mainPanel, "EntryMenu");
		}
	}

    
    private javax.swing.JLabel EditTitle;
    private javax.swing.JButton backButton;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JComboBox hourComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox minComboBox;
    private javax.swing.JComboBox rDateComboBox;
    private javax.swing.JComboBox rHourComboBox;
    private javax.swing.JComboBox rMinComboBox;
    private javax.swing.JComboBox rMonthComboBox;
    private javax.swing.JComboBox rTimeTypeComboBox;
    private javax.swing.JComboBox rYearComboBox;
    private javax.swing.JComboBox reminderComboBox;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox timeTypeComboBox;
    static javax.swing.JComboBox editTitleComboBox;
    private javax.swing.JTextField venueTextField;
}
